<?php
/*******************************************************************************
 * $Date:: Jan 7, 2011 6:09:31 PM                                              $
 * $Revision:                                                                  $
 * $Author:: Tuan Duong                                                        $
 * $Comment::                                                                  $
 ******************************************************************************/ 
/**
 * Description here...
 * 
 * @package Lua
 * @subpackage Router
 * @author Tuan Duong <tuan[at]smartdzine[dot]com>
 * @version 
 */

class Lua_Router extends Lua_Router_Abstract
{
    public function __construct()
    {
        $configs = Lua_Registry::get(Lua_Constant_General::LUA_CONFIG);
        $defaultController = isset($configs['lua']['default']['controller']) ? $configs['lua']['default']['controller'] : Lua_Constant_General::DEFAULT_CONTROLLER_NAME;
        $defaultAction = isset($configs['lua']['default']['action']) ? $configs['lua']['default']['controller'] : Lua_Constant_General::DEFAULT_ACTION_NAME;
        $request = Lua_Request::getInstance();
        
        $this->controller = $request->getParam(Lua_Constant_General::PARAM_CONTROLLER, $defaultController);
        $this->action = $request->getParam(Lua_Constant_General::PARAM_ACTION, $defaultAction);
        
        $uri = Lua_Uri::getInstance();
        $this->getModuleFromRequest($uri);
                
        $params = $uri->getQueryAsArray();
        if (isset($params[Lua_Constant_General::PARAM_MODULE])) {
            unset($params[Lua_Constant_General::PARAM_MODULE]);
        }

        if (isset($params[Lua_Constant_General::PARAM_CONTROLLER])) {
            unset($params[Lua_Constant_General::PARAM_CONTROLLER]);
        }

        if (isset($params[Lua_Constant_General::PARAM_ACTION])) {
            unset($params[Lua_Constant_General::PARAM_ACTION]);
        }
        
        $this->params = $uri->getQueryAsArray();
    }
    
    /**
     * @param Lua_Uri $uri
     */
    private function getModuleFromRequest(Lua_Uri $uri)
    {
        $configs = Lua_Registry::get(Lua_Constant_General::LUA_CONFIG);
        $defaultModule = isset($configs['lua']['default']['module']) ? $configs['lua']['default']['module'] : Lua_Constant_General::DEFAULT_MODULE_NAME;
        $scriptLocation = $this->getScriptLocation();
        
        if (empty($scriptLocation)) {
            $path = $uri->getPath();
        } else {
            $path = substr($uri->getPath(), strlen($scriptLocation));
        }
        
        // process default routes to getting module
        $items = explode('/', $path);
        
        // remove empty blocks
        foreach ($items as $key => $value) {
            if (strlen($value) == 0)
                unset($items[$key]);
        }
        
        if (empty($items)) {
            $this->module = $defaultModule;        
        } else {
            $this->module = array_shift($items); 
        }
    }
    
    /**
     * Build url from detail of this router
     * @param array $extras
     * @return string
     */
    public function url(array $extras = array())
    {
        $params = array();
        if (Lua_Constant_General::DEFAULT_MODULE_NAME != $this->getModule()) {
            $params[Lua_Constant_General::PARAM_MODULE] = $this->getModule();
        }
        if (Lua_Constant_General::DEFAULT_CONTROLLER_NAME != $this->getController()) {
            $params[Lua_Constant_General::PARAM_CONTROLLER] = $this->getController();
        }
        if (Lua_Constant_General::DEFAULT_ACTION_NAME != $this->getAction()) {
            $params[Lua_Constant_General::PARAM_ACTION] = $this->getAction();
        }
        $params = array_merge($params, $this->getParams(), $extras);
        return $this->buildUrl($params);
    }

    private function buildUrl(array $params)
    {
        $url = '/';
        if (isset($params[Lua_Constant_General::PARAM_MODULE])) {
            $url .= $params[Lua_Constant_General::PARAM_MODULE] . '/';
            unset($params[Lua_Constant_General::PARAM_MODULE]);
        }
        $pairs = array();
        foreach ($params as $key => $value) {
            $pairs[] = $key . '=' . $value;
        }
        if (0 < count($pairs)) {
            $url .= '?' . implode('&', $pairs);
        }

        $scriptLocation = $this->getScriptLocation();
        if (empty($scriptLocation)) {
            return $url;
        } else {
            return $scriptLocation . $url;
        }
    }

    public function getScriptLocation()
    {
        $phpScriptLocation = Lua_Uri::getInstance()->getServer('PHP_SELF');
        return substr($phpScriptLocation, 0, strrpos($phpScriptLocation, '/'));
    }
}